java provides a special feature called an adapter class that can simplify the creation of event handlers in certain situations Adapter classes are useful when you want to recive and process only sum of the events that are handed by a particular event listner interface. You can define a new class to act an event listner by extending one of the adapter class and implementing only those events in which you are interested.
Adapter classes |
Listener Interfaces |
ComponentAdapter |
ComponentListener |
ContainerAdapter |
ContainerListener |
FocusAdapter |
FocusListener |
KeyAdapter |
KeyListener |
MouseAdapter |
MouseListener |
MouseMotionAdapter |
MouseMotionListener |
WindowAdapter |
WindowListener |
WRITE A PROGRAM ON ADAPTER CLASS.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Adapter extends Applet
{
public void init()
{
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
}
}
class MyMouseAdapter extends MouseAdapter
{
adapter sam;
public MyMouseAdapter(Adapter sam)
{
this.sam=sam;
}
public void mouseClicked(MouseEvent m)
{
sam.showStatus("MouseClicked");
}
}
class MyMouseMotionAdapter extends MouseMotionAdapter
{
adapter sam;
public MyMouseMotionAdapter(Adapter sam)
{
this.sam=sam;
}
public void mouseDragged(MouseEvent m)
{
sam.showStatus("Dragged");
}
}
/*<applet code=Adapter width=200 height=100>
</applet>*/
WRITE A PROGRAM ON INNER CLASS.
import java.applet.*;
import java.awt.event.*;
public class Adapter1 extends Applet
{
public void init()
{
addMouseListener(new MyMouseAdapter());
}
class MyMouseAdapter extends MouseAdapter
{
public void mousePressed(MouseEvent m)
{
showStatus("Mouse Pressed");
}
}
}
/*<applet code=Adapter1 width=200 height=100>
</applet>*/
WRITE A PROGRAM ANONYMOUS INNER CLASS.
import java.applet.*;
import java.awt.event.*;
public class Adapter2 extends Applet
{
public void init()
{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent m) {
showStatus("Mouse Pressed");
}
});
}
}
/*<applet code=Adapter2 width=200 height=100>
</applet>*/